<template>
  <el-upload
      class="avatar-uploader"
      action="/system/file/uploadImage"
      :headers="{'Authorization':`Mine ${getToken()}`}"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload">
    <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
    <el-icon v-else class="avatar-uploader-icon">
      <Plus/>
    </el-icon>
  </el-upload>
</template>

<script>
import {getToken} from '@/utils/auth';
import {ElMessage} from "element-plus";
import {ref,getCurrentInstance} from 'vue'


const {Plus} = require("@element-plus/icons-vue/dist/index.mjs");
export default {
  name: "mine-upload",
  components: {Plus},
  emits:['uploadImgSuccess'],
  props: {
    imageUrl: {
      type: String,
      require: false,
      default: ''
    }
  },
  setup(props,context) {
    const {proxy} = getCurrentInstance()

    /**
     *  上传图片成功回调
     * @param response
     * @param uploadFile
     */
    const handleAvatarSuccess = (response, uploadFile) => {
      if (response.success) {
        context.emit('uploadImgSuccess',response.data)
      } else {
        ElMessage({
          message: response.msg,
          type: 'warning',
        })
      }
    }
    const beforeAvatarUpload = () => {

    }
    return {
      getToken,
      handleAvatarSuccess,
      beforeAvatarUpload
    }
  }
}
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 78px;
  height: 78px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #cbc5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  text-align: center;
}
</style>
